﻿using System;
using System.Text.RegularExpressions;
using System.Windows.Data;

namespace Red.Controls.Converters
{   
    // When we use menu items as other elements (e.g buttons), if their captions contain underscores,
    // they are treated by the menu as shortcut markers - here's the converter to properly escape those underscores.
    public class UnderscoreRepairerConverter : IValueConverter
    {
        public object Convert( object value, Type targetType, object parameter, System.Globalization.CultureInfo culture )
        {
            var s = value as string;
            // Use Regex, not the plain replace, because we don't want to already-double underscore to be escaped.
            return s != null ? Regex.Replace( s, "_", "__" ) : value;
        }

        public object ConvertBack( object value, Type targetType, object parameter, System.Globalization.CultureInfo culture )
        {
            return value;
        }
    }

}